
package mnstudent.global;

import mnstudent.datatranferobject.MajorOfSubjectDTO;
import mnstudent.datatranferobject.PrerequisiteSubjectDTO;
import mnstudent.datatranferobject.PriorityObjectDTO;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 11520_000
 */
public class Standard {
    
    public static int MAX_LEN = 255;
    public static int MAX_LEN_PHONE = 15;
    /**
     * Ham chuan hoa ten rieng nguoi
     * @param _str
     * @return 
     */
    public static String standardNamePerson(String _str)
    {
        try{
            _str = removeSpaceBalance(_str).toLowerCase();
            String temp[] = _str.split(" ");
            _str = "";
            for (int i = 0; i < temp.length; i++) {
                _str += String.valueOf(temp[i].charAt(0)).toUpperCase() + temp[i].substring(1);
                if (i < temp.length - 1) // ? ^-^
                    _str += " ";
            }
            return _str;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            return _str;
        } 
    }
    /**
     * Phuong thuc chuan hoa chuoi viet hoa chu cai dau tien
     * @param _str
     * @return 
     */
    public static String standardName(String _str)
    {
         try{
            _str = removeSpaceBalance(_str);
            String temp = _str.substring(0, 1).toUpperCase();
            _str = temp + _str.substring(1, _str.length());
            
            return _str;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            return _str;
        } 
    }
    /**
     * Ham bo cac ky tu trang thua
     * @param _str chuoi nguon
     * @return chuoi dich
     */
    public static String removeSpaceBalance(String _str)
    {
        String temp = _str.trim();
       
       temp = temp.replaceAll("\\s+", " ");
       return temp;
    }
    /**
     * Ham so sanh 2 chuoi
     * @param _str1
     * @param _str2
     * @return 
     */
    public static boolean compareToString(String _str1, String _str2)
    {
        if(_str1.toLowerCase().equals(_str2.toLowerCase()))
                return true;
        return false;
    }
    /**
     * Hàm cắt một chuỗi theo điều kiện
     * @param _str: chuỗi nguồn
     * @param _cond; chuỗi điều kiện truyền vào
     * @return 
     */
    public static String[] cutString(String _str, String _cond)
    {
        String[] temp = _str.split(_cond);
        return temp;
    }
    /**
     * Ham lay 2 so cuoi nam sinh vien nhap hoc
     * @param _str: nam sinh vien nhap hoc 
     * @return 
     */
    public static String cutToGetStudentID(String _str)
    {
        String temp = "";
        for(int i = _str.length()- 2; i<_str.length();++i)
        {
            temp+=_str.charAt(i);
        }
        return temp;
    }
    /**
     * Ham kiem tra chuoi str truyen vao co phai la so ko?
     * @param str
     * @return true: neu str la so, false neu ko phai
     */
    public static boolean isNumber(String str)
    {
        Pattern pattern = Pattern.compile("\\d*"); 
        Matcher matcher = pattern.matcher(str); 
        if (matcher.matches()) { 
            return true;
        } else { 
           return false;
        }
    }
    /**
     * Phuong thuc lay ngay gio he thong
     * @return 
     */
    public static Date getDateSystem()
    {      
        return new Date(System.currentTimeMillis());
    }
    /**
     * Phuong thuc chuyen ten cua cac phan tu trong array thanh string
     * @param arrObj
     * @return 
     */
    public static String getToStringOfArray(ArrayList<PriorityObjectDTO> arrObj)
    {
        String temp = "";
        if(arrObj.size()>0){
            for(int i = 0; i<arrObj.size(); i++)
                temp += (arrObj.get(i)).toString() +", ";
            temp = temp.substring(0, temp.length() - 2);
        }
        return temp;
    }

    /**
     * Phuong thuc ten cua danh sach Mon hoc tien quyet cua 1 mon hoc
     * @param arrObj
     * @return 
     */
    public static String getToStringOfArrayPrerequisiteSubject(ArrayList<PrerequisiteSubjectDTO> arrObj)
    {
        String temp = "";
        if(arrObj.size()>0){
            for(int i = 0; i<arrObj.size(); i++)
                temp += (arrObj.get(i)).toString() +", ";
            temp = temp.substring(0, temp.length() - 2);
        }
        return temp;
    }
    /**
     * Phuong thuc lay danh sach Nganh cua mot mon hoc
     * @param arrMajorOfSubjectChoosed
     * @return 
     */
    public static Object getToStringOfArrayMajorOfSubject(ArrayList<MajorOfSubjectDTO> arrMajorOfSubjectChoosed) {
         String temp = "";
        if(arrMajorOfSubjectChoosed.size()>0){
            for(int i = 0; i<arrMajorOfSubjectChoosed.size(); i++)
                temp += (arrMajorOfSubjectChoosed.get(i)).toString() +", ";
            temp = temp.substring(0, temp.length() - 2);
        }
        return temp;
    }

    /**
     * Phuong thuc tao ten dang nhap tu dong 
     * @param strName
     * @param birthday
     * @return 
     */
    public static String createUserName(String strName, int birthday)
    {
        String rs = "";
        strName = removeSpaceBalance(strName).toLowerCase();
        String temp[] = strName.split(" ");
        for(int i = 0; i < temp.length; i++)
        {
            if(i != temp.length - 1)
                rs += temp[i].substring(0, 1);
            else
                rs += temp[i];
        }
        rs += birthday;
        return rs;
    }
    /**
     * Phuong thuc so sanh 2 ngay
     * @param date1
     * @param date2
     * @return 
     */
    public static int compareDate(Date date1, Date date2)
    {
        if(date1.equals(date2))
            return 0;//2 ngay bang nhau
        else
            if(date1.before(date2))
                return 1;//ngay date1 truoc day 2
        else
            if(date1.after(date2))
               return 2;//ngay date1 sau ngay date2
        return -1;
    }

    public static int calculateAge(Date _date)
    {
        Calendar today = Calendar.getInstance();
        int age = today.get(Calendar.YEAR) - _date.getYear()-1900;
        if(today.get(Calendar.DAY_OF_YEAR) <= _date.getDay())
            return age - 1;
        return age;
    }
    
     public static int[] cutStringGetArrayInt(String _input)
    {
         if(_input.length()>0)
        {
            if(_input.indexOf('-')>=0)
            {
                String[] temp = _input.split("-");
                int[] intTemp = new int[temp.length];
                for(int i = 0; i<temp.length; i++)
                {
                    intTemp[i] = Integer.valueOf(temp[i].replaceAll("\\s+", ""));
                }
                return intTemp;
            }
        }
        return null;
    }
     
     public static String formatStringMoney(String money)
     {
         String temp = "";
         if(money.length()>=4){
            int length = money.length()/3+1;
            String[] arrTemp = new String[length];
            
            for(int i = 0; i < length; i = i+3)
            {
               System.out.print(money.substring(i, i+2));
            }
         }
         return temp;
     }
}
